﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.IO.Ports;
using Bentley.Geometry;
using Bentley.GenerativeComponents;
using Bentley.GenerativeComponents.Transactions;
using Bentley.GenerativeComponents.Features;
using Bentley.GenerativeComponents.Features.Specific;
using Bentley.GenerativeComponents.Features.FeatureTypeGenerator;
using Bentley.GenerativeComponents.GCScript;
using Bentley.GenerativeComponents.GeneralPurpose;
using Bentley.GenerativeComponents.Transactions.GraphChange;
using Bentley.GenerativeComponents.Transactions.FeatureTypeGenerator;
using Bentley.GenerativeComponents.Transactions.IncludeFile;
using Bentley.GenerativeComponents.Transactions.Script;
using PubSubSubscriber;

namespace UbiMashGCFeatures
{
    public partial class UbiMashSubscriberUI : Form, UbiMashListener
    {
        //private Subscriber sub;        
        //private string msgReceived;
        private delegate void SetTextBoxCallback(string text, TextBox txtbox);
        private delegate void SetListBoxCallback(string text, ListBox listbox);
        //private string commData;
        UbiMashSubscriber _uSubscriber;

        public UbiMashSubscriberUI(ref UbiMashSubscriber uSubscriber)
        {
            InitializeComponent();
            _uSubscriber = uSubscriber;
            _uSubscriber.ConnectToUbiMash(txtSubIPAdd.Text, int.Parse(txtSubPortNo.Text));
            _uSubscriber.AddListener(this);
        }

        public void refresh(string ptListStr)
        {
            this.txtInMessages.AppendText(ptListStr + Environment.NewLine);
        }

        //internal string ExpressionText
        //{
        //    get
        //    {
        //        return "\"" + commData + "\"";
        //    }
        //    set
        //    {
        //        value = "";
        //    }
        //}

        private void btnSubscribe_Click(object sender, EventArgs e)
        {
            //if (lstTopic.SelectedItem == null || lstTopic.SelectedItems.Count == 0)
            //{
            //    MessageBox.Show("Please select a topic name in the list box");
            //}
            //else
            //{
            //    sub = new Subscriber(txtSubIPAdd.Text, int.Parse(txtSubPortNo.Text));
            //    sub.messageChanged += UpdateMessage;
            //    Subscribe(lstTopic.SelectedIndex.ToString());
            //}
        }

        private void btnUnsubscribe_Click(object sender, EventArgs e)
        {
            //Unsubscribe(lstTopic.SelectedIndex.ToString());
        }

        //private void UpdateMessage(object sender, MessageChangedArgs args)
        //{
        //    try
        //    {
        //        msgReceived = args.message;

        //        Debug.WriteLine("message received: " + msgReceived);
        //        Debug.Flush();                           

        //        if (this.txtInMessages.InvokeRequired)
        //        {
        //            SetTextBoxCallback st = new SetTextBoxCallback(SetTextBox);
        //            this.Invoke(st, new object[] { msgReceived, this.txtInMessages });
        //        }
        //        else
        //        {
        //            this.txtInMessages.AppendText(msgReceived + Environment.NewLine); 
        //        }

        //        Debug.WriteLine("message appended in text box: " + msgReceived);
        //        Debug.Flush();

        //        string[] str = msgReceived.Trim().Split(',');

        //        if ( (str[1] != null && str[1] != ""))
        //        {                       
        //            string s = str[1].Trim();
                    
        //            //string format: "{0:1:2;3:4:5;}"
        //            // remove {}
        //            s = s.Substring(1);
        //            s = s.Substring(0, (s.Length - 1));

        //            string[] pts = s.Split(';');
        //            double[][] ptList = new double[pts.Length][];

        //            if (pts[pts.Length -1] == "")
        //            {
        //               ptList = new double[pts.Length - 1][];                        
        //            }
                    
        //            for (int i = 0; i < ptList.Length; i++)
        //            {
        //                string[] xyz = pts[i].Split(':');
        //                if (xyz.Length == 3)
        //                {
        //                    try
        //                    {
        //                        ptList[i] = new double[3];
        //                        ptList[i][0] = Convert.ToDouble(xyz[0]);
        //                        ptList[i][1] = Convert.ToDouble(xyz[1]);
        //                        ptList[i][2] = Convert.ToDouble(xyz[2]);
        //                    }
        //                    catch (Exception castex)
        //                    {
        //                        MessageBox.Show("Exception in converting messages to point list: you can only send numbers" + Environment.NewLine + castex.Message);
                                
        //                    }
        //                }
        //            }
                    
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        Debug.WriteLine(e.Message);
        //        Debug.WriteLine(e.StackTrace);
        //    }
        //}

        // This method is passed in to the SetTextCallBack delegate to set the Text property of Label lbl
        private void SetTextBox(string text, TextBox txtbox)
        {
            txtbox.AppendText(text + Environment.NewLine);            
        }

        //// This method is passed in to the SetTextCallBack delegate to set the Text property of Label lbl
        //private void SetListBox(string text, ListBox listbox)
        //{
        //    lstTopic.Items.Add(text);
        //}

        private void btnConnect_Click(object sender, EventArgs e)
        {
            _uSubscriber.ConnectToUbiMash(txtSubIPAdd.Text, int.Parse(txtSubPortNo.Text));
        }
        
        //public void DisconnectFromUbimash()
        //{
        //    sub.Shutdown();
        //    Debug.WriteLine("Subscriber shut down");
        //    Debug.Flush();
        //}

    }
}
